/*
 * 支付界面
 */
cc.Class({
    extends: cc.Component,

    properties: {
       
        labLottery:{
            default:null,
            type:cc.Label
        },
        //订单金额
        labOrderMoney:{
            default:null,
            type:cc.Label,
        },
        //余额
        labBalance:{
            default:null,
            type:cc.Label
        },
        //实际支付
        labPayMoney:{
            default:null,
            type:cc.Label
        },

        fbRechargePop:cc.Prefab,//充值界面
        fbWechatPop:cc.Prefab,

        ndNeedPay:cc.Node,//还需支付
        ndPayWays:cc.Node,//支付路径
        ndTrue:cc.Node,//确认支付
        
        _orderMoney:0,//订单金额
        _lotteryID:0,//彩种编号
        _buyType:0,//投注类型

        _userBanl:0,//用户余额
        _needPayMoney:0,//还需要支付金额
        _balance:0,//支付的金额
        _payType:0,//充值类型 0：微信 1：支付宝

        payCallback:null,
    },

    // use this for initialization
    start: function () {
    },

//初始化
    init:function(lotteryId, isuseid, moneyAmount,buytype, isUseCoupons,payCallback){
        this.payCallback = payCallback;
        this._orderMoney = parseFloat(moneyAmount);//订单金额
        this._needPayMoney = this._orderMoney;//需要支付的金额
        this._userBanl = parseFloat(User.getBalance());//余额
        this._lotteryID = lotteryId;
        this._buyType = buytype;

        var lotteryDecStr = LotteryUtils.getLotteryTypeDecByLotteryId(lotteryId)+" 第"+isuseid.toString()+"期";
        var orderAmountStr = this._orderMoney.toFixed(2).toString()+"元宝"; //订单
        this.labLottery.string = lotteryDecStr;
        this.labOrderMoney.string = orderAmountStr;

        this.payShow();
    },

    //刷新余额支付显示
    payShow:function(){
        if(this._needPayMoney > 0)
        {
            var baTemp = this._userBanl - this._needPayMoney;
            if(baTemp >=0)
            {
                this._balance = this._needPayMoney.toFixed(2);
                this.labBalance.string = "-" + this._needPayMoney.toFixed(2).toString() + "元宝";
                this.labPayMoney.string = "0.00元宝";//还需支付
                this._needPayMoney = 0;
                this.ndTrue.active = true;
                this.ndNeedPay.active = false;
                this.ndPayWays.active = false;
            }
            else
            {
                this._balance = this._userBanl.toFixed(2);
                this._needPayMoney = 0-baTemp;
                this.labBalance.string = "-" + this._userBanl.toFixed(2).toString() + "元宝";//余额抵扣
                this.labPayMoney.string = baTemp.toFixed(2).toString() + "元宝";//还需支付
                this.ndTrue.active = false;
                this.ndNeedPay.active = true;
                this.ndPayWays.active = true;
            }
        }
        else
        {
            this._needPayMoney = 0;
            this._balance = 0;
            this.labPayMoney.string = "0.00元宝";
            this.labBalance.string = "-0.00元宝";
            this.ndTrue.active = true;
            this.ndNeedPay.active = false;
            this.ndPayWays.active = false;
        }
    },

    onPayTrue:function(){
        var data = {
            paymentType:2,
            data:"",
            payMoney:LotteryUtils.moneytoClient(this._balance),
            gold:0,
        }
      
        this.payCallback(data);//支付成功回调
        this.onClose();
    },

    //关闭
    onClose:function(event){
        this.node.getComponent("Page").backAndRemove();
    },

    //下一步
    onNext:function(){
        //ComponentsUtils.showTips("暂时无法使用充值支付");
        if(this._payType == "0")
        {
            //ComponentsUtils.showTips("暂时无法使用微信充值支付");
            var self = this;
            var recv = function(ret){
                ComponentsUtils.unblock();
                if (ret.Code !== 0) {
                    cc.error(ret.Msg);
                    ComponentsUtils.showTips(ret.Msg);
                }else{
                    var canvas = cc.find("Canvas");
                    var wechatPop = cc.instantiate(this.fbWechatPop);
                    var url = ret.Data.PayLink;
                    wechatPop.getComponent(wechatPop.name).init(url);
                    canvas.addChild(wechatPop);
                }
            }.bind(this);
    
            var data = {
                Token: User.getLoginToken(),
                UserCode: User.getUserCode(),
                Amount: LotteryUtils.moneytoClient(this._needPayMoney),
                TbBody: "wxpay",
                TbAttach:"wxpay"
            };
            CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.WXPAY, data, recv.bind(this),"POST");  
            ComponentsUtils.block();
        }
        else if(this._payType == "1")
        {
            var recv = function(ret){
                ComponentsUtils.unblock();
                if (ret.Code !== 0) {
                    cc.error(ret.Msg);
                    ComponentsUtils.showTips(ret.Msg);
                }else{
                    //cc.log('支付',ret.Data);
                    var canvas = cc.find("Canvas");
                    var rechargePop = cc.instantiate(this.fbRechargePop);
                    var url = ret.Data.PayLink;
                    rechargePop.getComponent(rechargePop.name).init(url);
                    canvas.addChild(rechargePop);
                }
            }.bind(this);
    
            var data = {
                Token: User.getLoginToken(),
                UserCode:  User.getUserCode(),
                Amount:LotteryUtils.moneytoClient( this._needPayMoney ),
                TbBody: "alipay",
                TbAttach:"alipay"
            };
            CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.ALIPAY, data, recv.bind(this),"POST");  
            ComponentsUtils.block();
        }
    },

    onSelPay:function(toggle, customEventData){
        this._payType = customEventData;
    }

});
